﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BackGround_CheckInfo : System.Web.UI.Page
{
    Operation operation = new Operation(); //业务类对象
    static string infoType = "";　　　　　 //信息类型
    static bool isSorting = false;
    static int t = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GridView1.Attributes.Add("SortExpression", "date");
            this.GridView1.Attributes.Add("SortDirection", "DESC");

            if (Session["Admin"] == null)
            {
                WebMessageBox.Show("会话失效，请重新登录！");
            }
            else
            {
                infoType = Request.QueryString["id"].ToString();
                ListItem it = new ListItem();
                it.Text = infoType;
                it.Value = "0";
                dlCategory.Items.Add(it);
                if (infoType.IndexOf("技术") != -1)
                {
                    BuildDownList("0", "", "---");
                }
                rdoBtnCheckTrue.Text = "已审核" + dlCategory.SelectedItem.Text;
                rdoBtnCheckFalse.Text = "未审核" + dlCategory.SelectedItem.Text;
                rdoBtnCheckAll.Text = "显示所有" + infoType;
                sl_Change(new object(), new EventArgs());
            }
        }
    }
    public void BuildDownList(string pid, string itH, string dx)//itH为表项前度，dx为递增前度
    {
        DataSet ds = operation.SelectCategoryByPID(pid);//一级类别
        int count = ds.Tables[0].Rows.Count;
        string t = itH + dx;//下级前度
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Value = ds.Tables[0].Rows[i][0].ToString();
            item.Text = itH + ds.Tables[0].Rows[i][1].ToString();
            dlCategory.Items.Add(item);
            BuildDownList(item.Value, t, dx);
        }
    }
    /// <summary>
    /// 绑定供求信息到GridViev控件
    /// </summary>
    /// <param name="type">供求信息类别</param>
    private void GridViewBind(string type)
    {
        GridView1.DataSource = operation.SelectInfo(type);
        GridView1.DataKeyNames=new string[] {"id"};
        GridView1.DataBind();
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //高亮显示指定行
            e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#FFF000'");
            e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");
            //设置审核状态，并且设置相应的颜色。
            if (e.Row.Cells[4].Text == "False")
            {
                e.Row.Cells[4].Text =StringFormat.HighLight("未审核",true);
            }
            else
            {
                e.Row.Cells[4].Text = StringFormat.HighLight("已审核", false);
            }
            if (e.Row.Cells[0].Text.CompareTo("0") == 0)
            {
                e.Row.Cells[0].Text = infoType;
            }
            else
            {
                try
                {
                    e.Row.Cells[0].Text = operation.SelectCategoryByID(e.Row.Cells[0].Text).Tables[0].Rows[0][1].ToString();
                }
                catch (Exception ex) { }
            }
            //多余字　使用...显示
            e.Row.Cells[2].Text = StringFormat.Out(e.Row.Cells[2].Text, 18);
            //删除指定行数据时，弹出询问对话框
            ((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick", "return confirm('是否删除当前行数据！')");
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //WebMessageBox.Show("" + e.NewSelectedIndex);
        string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        operation.UpdateInfo(id, infoType);
        //按审核类型绑定数据（３种类别：全部显示，显示未审核，显示审核）
        if (rdoBtnCheckAll.Checked)
            GridViewBind(infoType);
        else if (rdoBtnCheckFalse.Checked)
        {
            if (dlCategory.Text.CompareTo("0") == 0)
                GridView1.DataSource = operation.SelectInfo(infoType, false);
            else
                GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, false);
            GridView1.DataBind();
        }
        else if (rdoBtnCheckTrue.Checked)
        {
            if (dlCategory.Text.CompareTo("0") == 0)
                GridView1.DataSource = operation.SelectInfo(infoType, true);
            else
                GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, true);
            GridView1.DataBind();
        }
    }
    
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        operation.DeleteInfo(GridView1.DataKeys[e.RowIndex].Value.ToString());     
        
        //如果有相关的新闻图片则删除;
        if (GridView1.Rows[e.RowIndex].Cells[0].Text.CompareTo("新闻信息") == 0)
        {
            if (System.IO.File.Exists(Server.MapPath("im/" + GridView1.DataKeys[e.RowIndex].Value.ToString() + ".jpg")))
            {
                System.IO.File.Delete(Server.MapPath("im/" + GridView1.DataKeys[e.RowIndex].Value.ToString() + ".jpg"));
            }
        }
        sl_Change(new object(), new EventArgs());
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //分页设置
        GridView1.PageIndex = e.NewPageIndex;
        //按审核类型绑定数据（３种类别：全部显示，显示未审核，显示审核）
        if (rdoBtnCheckAll.Checked)
            GridViewBind(infoType);
        else if (rdoBtnCheckFalse.Checked)
        {
            if (dlCategory.Text.CompareTo("0") == 0)
                GridView1.DataSource = operation.SelectInfo(infoType, false);
            else
                GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, false);
            GridView1.DataBind();
        }
        else if (rdoBtnCheckTrue.Checked)
        {
            if (dlCategory.Text.CompareTo("0") == 0)
                GridView1.DataSource = operation.SelectInfo(infoType, true);
            else
                GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, true);
            GridView1.DataBind();
        }
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
    }
    protected void sl_Change(object sender, EventArgs e)//按类别显示
    {
        int i = dlCategory.SelectedItem.Text.LastIndexOf("-");
        
        if (i == -1)
        {
            rdoBtnCheckFalse.Text = "未审核" + dlCategory.SelectedItem.Text;
            rdoBtnCheckTrue.Text = "已审核" + dlCategory.SelectedItem.Text;
        }
        else
        {
            rdoBtnCheckFalse.Text = "未审核" + dlCategory.SelectedItem.Text.Substring(i+1)+"类";
            rdoBtnCheckTrue.Text = "已审核" + dlCategory.SelectedItem.Text.Substring(i + 1) + "类";
        }
        //按审核类型绑定数据（３种类别：全部显示，显示未审核，显示审核）
        if(rdoBtnCheckAll.Checked)
            GridViewBind(infoType);
        else if (rdoBtnCheckFalse.Checked)
        {
            if (dlCategory.Text.CompareTo("0") == 0)
                GridView1.DataSource = operation.SelectInfo(infoType, false);
            else
                GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, false);
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();
        }
        else if (rdoBtnCheckTrue.Checked)
        {
            if (dlCategory.Text.CompareTo("0") == 0)
                GridView1.DataSource = operation.SelectInfo(infoType, true);
            else
                GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, true);
            GridView1.DataKeyNames = new string[] { "id" };
            // GridView绑定并显示数据
            GridView1.DataBind();
        }
        
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
        //dlCategory.Enabled = true;
    }
    protected void rdoBtnCheckTrue_CheckedChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
        if (dlCategory.Text.CompareTo("0") == 0)
            GridView1.DataSource = operation.SelectInfo(infoType, true);
        else
            GridView1.DataSource = operation.SelectInfo(infoType,dlCategory.SelectedValue, true);
        GridView1.DataBind();
        
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
        dlCategory.Enabled = true;
    }
    protected void rdoBtnCheckFalse_CheckedChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
        if (dlCategory.Text.CompareTo("0") == 0)
            GridView1.DataSource = operation.SelectInfo(infoType, false);
        else
            GridView1.DataSource = operation.SelectInfo(infoType, dlCategory.SelectedValue, false);
        GridView1.DataBind();
        
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
        dlCategory.Enabled = true;
    }
    protected void rdoBtnCheckAll_CheckedChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
        GridViewBind(infoType);
        
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
        dlCategory.Enabled = false;
    }
    protected void sorted(object sender, GridViewSortEventArgs e)
    {
        //if (isSorting) return;
       // isSorting = true;
        
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();
        // 假定为排序方向为“顺序”
        //SortDirection sortDirection = SortDirection.Ascending;
        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        string sortDirection = "ASC";
        if (this.GridView1.Attributes["SortExpression"].CompareTo(sortExpression)==0)
        {
            //获得下一次的排序状态
            if (this.GridView1.Attributes["SortDirection"].CompareTo(sortDirection) == 0)
            {
                sortDirection = "DESC";
            }
        }
        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;
        // 根据GridView排序数据列及排序方向设置显示的默认数据视图
        //WebMessageBox.Show("" + sortExpression);
        if (!string.IsNullOrEmpty(sortExpression))
        {
            sl_Change(new object(), new EventArgs());
            DataTable dt = ((DataSet)GridView1.DataSource).Tables[0];

            DataView dv = new DataView(dt);
            dv.Sort = string.Format("{0} {1}", sortExpression, sortDirection);

            GridView1.DataSource = dv;
            GridView1.DataBind();         
        }
    }
}
